package com.clz.appdesign.appdependence.base;

import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.clz.appdesign.appdependence.widget.LoadingDialog;
import com.trello.rxlifecycle2.components.RxFragment;

import rx.subscriptions.CompositeSubscription;

/**
 * @author chenliangzhi
 * @date 2018/1/10
 * @describe
 */

public abstract class BaseFragment extends RxFragment {
    protected CompositeSubscription compositeSubscription;
    protected LoadingDialog loadingDialog;
    private static final String STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN";
    private View mContextView = null;

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeInit();
        compositeSubscription = new CompositeSubscription();
        // “内存重启”时调用  解决重叠问题  #0001
        if (savedInstanceState != null) {
            boolean isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (isSupportHidden) {
                ft.hide(this);
            } else {
                ft.show(this);
            }
            ft.commit();
        }
    }

    /**
     * 界面初始化前期准备
     */
    protected void beforeInit() {

    }

    public void show2Loading() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    public void hide2Loading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mContextView = inflater.inflate(bindLayout(), container, false);
        initView(mContextView);
        doBusiness(getActivity());
        return mContextView;
    }

    /**
     * [绑定布局]
     *
     * @return
     */
    public abstract int bindLayout();

    /**
     * [初始化控件]
     *
     * @param view
     */
    public abstract void initView(final View view);

    /**
     * [业务操作]
     *
     * @param mContext
     */
    public abstract void doBusiness(Context mContext);

    /**
     * 日志输出
     *
     * @param msg
     */
    public void TLog(String msg) {
        if (AppUtils.isAppDebug()) {
            LogUtils.d(msg);
        }
    }

    public void toast(String text) {
        ToastUtils.showShort(text);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (compositeSubscription != null
                && !compositeSubscription.isUnsubscribed()) {
            compositeSubscription.unsubscribe();
        }
        if (loadingDialog != null) {
            loadingDialog = null;
        }
    }
}
